﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SocialLearning
{
    public class UAC_GroupManagement
    {
        #region Private Fields

        //private UAC_GroupManagement_Control gmControl = new UAC_GroupManagement_Control();
        private static UAC_GroupManagement instance;
        private static List<List<string>> Groups = new List<List<string>>();
        private static List<string> newGroup;
        private static List<string> GroupNames = new List<string>();

        #endregion

        #region Property

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static UAC_GroupManagement Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new UAC_GroupManagement();
                }
                return instance;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Creates the specified group name.
        /// </summary>
        /// <param name="GroupName">Name of the group.</param>
        /// <returns></returns>
        public bool Create(string GroupName)
        {
            GroupNames.Add(GroupName);
            Groups.Insert(GroupNames.IndexOf(GroupName), newGroup = new List<string>());
            return true;
        }

        /// <summary>
        /// Adds the member.
        /// </summary>
        /// <param name="GroupName">Name of the group.</param>
        /// <param name="UserID">The user ID.</param>
        /// <returns></returns>
        public bool AddMember(string GroupName, string UserID)
        {
            bool check = false;
            List<string> templist;
            foreach (var VARIABLE in GroupNames)
            {
                if (GroupName == VARIABLE)
                {
                    templist = Groups[GroupNames.IndexOf(GroupName)];
                    templist.Add(UserID);
                    Groups[GroupNames.IndexOf(GroupName)] = templist;
                    check = true;
                }
            }
            return check;
        }

        /// <summary>
        /// Searches the group.
        /// </summary>
        /// <param name="GroupName">Name of the group.</param>
        /// <returns></returns>
        public bool SearchGroup(string GroupName)
        {
            bool check = false;
            foreach (var VARIABLE in GroupNames)
            {
                if (GroupName == VARIABLE)
                {
                    check = true;
                }
            }
            return check;
        }

        /// <summary>
        /// Searches the group member.
        /// </summary>
        /// <param name="UserID">The user ID.</param>
        /// <returns></returns>
        public List<string> SearchGroupMember(string UserID)
        {
            List<string> tempGroup = new List<string>();
            List<string> templist = null;
            if (GroupNames != null)
            {
                foreach (var VARIABLE in GroupNames)
                {
                    templist = Groups[GroupNames.IndexOf(VARIABLE)];
                    foreach (string VARIABLE2 in templist)
                    {
                        if (VARIABLE2 == UserID)
                        {
                            tempGroup.Add(VARIABLE);
                        }
                    }
                }
            }
            return tempGroup;
        }

        /// <summary>
        /// Removes the group.
        /// </summary>
        /// <param name="GroupName">Name of the group.</param>
        /// <returns></returns>
        public bool RemoveGroup(string GroupName)
        {
            bool check = false;

            foreach (var VARIABLE in GroupNames)
            {
                if (GroupName == VARIABLE)
                {
                    Groups.RemoveAt(GroupNames.IndexOf(VARIABLE));
                    GroupNames.RemoveAt(GroupNames.IndexOf(VARIABLE));
                    check = true;
                    break;
                }
            }
            return check;
        }

        /// <summary>
        /// Removes the group member.
        /// </summary>
        /// <param name="UserID">The user ID.</param>
        /// <returns></returns>
        public bool RemoveGroupMember(string UserID)
        {
            bool check = false;
            List<string> templist = null;
            if (GroupNames != null)
            {
                foreach (var VARIABLE in GroupNames)
                {
                    templist = Groups[GroupNames.IndexOf(VARIABLE)];
                    foreach (var VARIABLE2 in templist)
                    {
                        if (VARIABLE2 == UserID)
                        {
                            templist.RemoveAt(templist.IndexOf(UserID));
                            check = true;
                            break;
                        }
                    }
                }
            }
            return check;
        }

        /// <summary>
        /// Sees the group names.
        /// </summary>
        /// <returns></returns>
        public List<string> SeeGroup()
        {
            return GroupNames;
        }

        /// <summary>
        /// Sees the group members.
        /// </summary>
        /// <param name="GroupName">Name of the group.</param>
        /// <returns></returns>
        public List<string> SeeGroupMembers(string GroupName)
        {
            List<string> templist = null;
            foreach (var VARIABLE in GroupNames)
            {
                if (GroupName == VARIABLE)
                {
                    templist = Groups[GroupNames.IndexOf(GroupName)];
                }
            }
            return templist;
        }

        /// <summary>
        /// Updates the group.
        /// </summary>
        /// <param name="GroupName">Name of the group.</param>
        /// <param name="NewGroupName">New name of the group.</param>
        /// <returns></returns>
        public bool UpdateGroup(string GroupName, string NewGroupName)
        {
            bool check = false;
            foreach (var VARIABLE in GroupNames)
            {
                if (GroupName == VARIABLE)
                {
                    GroupNames[GroupNames.IndexOf(GroupName)] = NewGroupName;
                    check = true;
                    break;
                }
            }
            return check;
        }

        #endregion

        #region Engine Implementation



        #endregion

        #region UAC_GroupManagement_Control Implementation

        //// Get Single Group
        //public void GetGroupSingle(string groupName)
        //{
        //    var tempGroup = gmControl.GetUAC_GroupManagement(groupName);
        //}

        //// Get Group List
        //public void LoadGroupList()
        //{
        //    Groups = gmControl.GetUAC_GroupManagements();
        //}

        //// Update or Save Group
        //public void UpdateSave(UAC_GroupManagement group)
        //{
        //    gmControl.SaveOrUpdateUAC_GroupManagement(group);
        //}

        //// Delete Group
        //public void DeleteUAC_GroupManagement(UAC_GroupManagement group)
        //{
        //    gmControl.DeleteUAC_GroupManagement(group);
        //}

        #endregion
    }
}
